﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SgsCS
{
    internal class CardPile : IComparer<int>
    {
        private static List<CardSet> myCardPile;
        private List<int> pile;
        private bool _mode = false;
        private int _limitLength = 0;

        public CardPile()
        {
            if (myCardPile == null)
                myCardPile = new List<CardSet>();
            pile = new List<int>();
        }

        public bool LimitMode
        {
            set { _mode = value; }
            get { return _mode; }
        }

        public int LimitLength
        {
            set
            {
                _mode = true;
                _limitLength = value;
            }
            get { return _limitLength; }
        }


        public int Push(int index)
        {
            if (_mode)
            {
                int result = -1;
                if (pile.Count >= _limitLength)
                {
                    result = pile[0];
                    pile.RemoveAt(0);
                }
                pile.Add(index);
                return result;
            }
            else
            {
                pile.Add(index);
                return pile.Count - 1;
            }
        }

        public int Pop(int index)
        {
            return pile[index];
        }

        public void Shuffle(int count)
        {
            if (count < 0) return;
            if (pile.Count < count) count = pile.Count - 1;
            Shuffle(0, count);
        }

        public void Shuffle()
        {
            if (pile.Count < 1) return;
            Shuffle(0, pile.Count - 1);
        }

        public void Shuffle(int sIndex, int eIndex)
        {
            if (eIndex < 0) eIndex = 0;
            if (sIndex >= pile.Count) sIndex = pile.Count - 1;
            if (eIndex == sIndex) return;
            if (eIndex < sIndex)
            {
                eIndex += sIndex;
                sIndex = eIndex - sIndex;
                eIndex = eIndex - sIndex;
            }
            int total = eIndex - sIndex;
            int lenght = (int)(total / 2);
            for (int i = 0; i < lenght; i++)
            {
                Random rnd = new Random();
                int index = rnd.Next(total - i);
                int tempInt = pile[sIndex + index];
                pile[sIndex + index] = pile[eIndex];
                pile[eIndex] = tempInt;
            }
        }

        public void Sort(int sIndex,int eIndex)
        {
            if (sIndex < 0) return;
            if (pile.Count < 1) return;
            if (eIndex >= pile.Count) return;
            if (sIndex > eIndex)
            {
                sIndex += eIndex;
                eIndex = sIndex - eIndex;
                sIndex = sIndex - eIndex;
            }
            pile.Sort(sIndex, eIndex - sIndex, this);
        }

        public void Sort()
        {
            Sort(0, pile.Count - 1);
        }

        public static CardSet Item(int index)
        {
            return myCardPile[index];
        }

        public static int Add(CardSet card)
        {
            myCardPile.Add(card);
            return myCardPile.Count - 1;
        }

        public static CardSet Remove(int index)
        {
            CardSet card = myCardPile[index];
            myCardPile.RemoveAt(index);
            return card;
        }
        
        public int Compare(int x, int y)
        {
            int tempX = Convert.ToInt32(myCardPile[x].CardType.CardType);
            int tempY = Convert.ToInt32(myCardPile[y].CardType.CardType);
            if (tempX == tempY) return 0;
            if (tempX < tempY) return 1;
            return -1;
        }
    }
}
